/*
 * LoadGame.java
 *
 * Created on Apr 22, 2010, 1:31:29 PM
 */

package controller.persiapan;
import controller.permainan.*;
import helper.IOSavedGame;
import model.Game;

public class LoadGame extends javax.swing.JFrame{

    private Persiapan parent;

    /** Creates new form LoadGame */
    public LoadGame(Persiapan _parent) {
        initComponents();
        parent = _parent;
        LoadSavedFile();

    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        back_button = new javax.swing.JButton();
        load_button = new javax.swing.JButton();
        savedgame_pane = new javax.swing.JScrollPane();
        savedgame_list = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        players_textarea = new javax.swing.JTextArea();
        delete_button = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Load Game");

        back_button.setText("<< Back");
        back_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                back_buttonActionPerformed(evt);
            }
        });

        load_button.setText("Load");
        load_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                load_buttonActionPerformed(evt);
            }
        });

        savedgame_list.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        savedgame_list.setFont(new java.awt.Font("Hand Me Down S (BRK)", 0, 20)); // NOI18N
        savedgame_list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                savedgame_listValueChanged(evt);
            }
        });
        savedgame_pane.setViewportView(savedgame_list);

        players_textarea.setColumns(20);
        players_textarea.setFont(new java.awt.Font("Andalus", 0, 18)); // NOI18N
        players_textarea.setRows(5);
        jScrollPane1.setViewportView(players_textarea);

        delete_button.setText("Delete");
        delete_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_buttonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("French Script MT", 0, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Saved Game Files");

        jLabel2.setFont(new java.awt.Font("French Script MT", 0, 36)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Players");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(back_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(delete_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(savedgame_pane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(load_button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(savedgame_pane, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(back_button, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(load_button, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(delete_button, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadSavedFile()
    {
        this.savedgame_list.setModel(new javax.swing.AbstractListModel()
        {
            String[] filelist = IOSavedGame.getSavedgameList();

            public int getSize()
            {
                return filelist.length;
            }

            public Object getElementAt(int index)
            {
                return filelist[index];
            }
        });
        this.savedgame_pane.setViewportView(this.savedgame_list);
        this.savedgame_pane.repaint();

    }
    private void back_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_buttonActionPerformed
        // TODO add your handling code here:
        this.dispose();
        parent.setVisible(true);
    }//GEN-LAST:event_back_buttonActionPerformed

    private void load_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_load_buttonActionPerformed
        if (savedgame_list.getSelectedValue() != null)
        {
            String filename = new String (savedgame_list.getSelectedValue().toString());
            Game temp = (Game) IOSavedGame.Load(filename);
            this.dispose();
            new Permainan2(temp).setVisible(true);
        }
        else
        {
            javax.swing.JOptionPane.showMessageDialog(this, "Saved game list is empty", "Load Game Warning", javax.swing.JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_load_buttonActionPerformed

    private void savedgame_listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_savedgame_listValueChanged
        if (savedgame_list.getSelectedValue() != null)
        {
            String filename = new String (savedgame_list.getSelectedValue().toString());
            Game temp = (Game) IOSavedGame.Load(filename);
            players_textarea.setText(temp.Q.PrintQueue(0));
        }
        
    }//GEN-LAST:event_savedgame_listValueChanged

    private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_buttonActionPerformed
        if (savedgame_list.getSelectedValue() != null)
        {
            String delfilename = new String (savedgame_list.getSelectedValue().toString());
            IOSavedGame.delSavedgame(delfilename);
            LoadSavedFile();
            savedgame_list.setSelectedIndex(0);
        }
        else
        {
            javax.swing.JOptionPane.showMessageDialog(this, "Saved game list is empty", "Load Game Warning", javax.swing.JOptionPane.WARNING_MESSAGE);
        }

}//GEN-LAST:event_delete_buttonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoadGame(new Persiapan()).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton back_button;
    private javax.swing.JButton delete_button;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton load_button;
    private javax.swing.JTextArea players_textarea;
    private javax.swing.JList savedgame_list;
    private javax.swing.JScrollPane savedgame_pane;
    // End of variables declaration//GEN-END:variables

}
